package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class LongestCommonPrefix {
    // 2022-09-02 09:47:55
    public static void main(String[] args) {
        Solution solution = new LongestCommonPrefix().new Solution();
        solution.longestCommonPrefix(new String[]{"flower", "flow", "flight"});
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0) return "";
            if (strs.length == 1) return strs[0];
            int ansLength = 300;
            String str = strs[0];
            if (str.equals("")) return "";
            for (int i = 1; i < strs.length; i++) {
                int index = 0;
                while (index < strs[i].length() && index < str.length()) {
                    if (strs[i].charAt(index) != str.charAt(index)) {
                        break;
                    }
                    index++;
                }
                ansLength = Math.min(index, ansLength);
            }
            if (ansLength == 300) {
                return "";
            }
            return str.substring(0, ansLength);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}